from .extensions import db, login_manager
from flask_login import UserMixin
from datetime import datetime
import json

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    password_hash = db.Column(db.String(120), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    alerts = db.relationship('Alert', backref='user', lazy=True)
    
    def __repr__(self):
        return f'<User {self.username}>'

class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text, nullable=False)
    url = db.Column(db.String(500), nullable=False)
    source = db.Column(db.String(100), nullable=False)
    sentiment = db.Column(db.String(20), nullable=True)
    keywords = db.Column(db.String(500), nullable=True)
    published_at = db.Column(db.DateTime, nullable=True)
    collected_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def set_keywords(self, keywords_list):
        self.keywords = json.dumps(keywords_list)
    
    def get_keywords(self):
        return json.loads(self.keywords) if self.keywords else []
    
    def __repr__(self):
        return f"Article('{self.title}', '{self.source}')"

class SocialMediaPost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    platform = db.Column(db.String(50), nullable=False)
    content = db.Column(db.Text, nullable=False)
    author = db.Column(db.String(100), nullable=True)
    url = db.Column(db.String(500), nullable=True)
    sentiment = db.Column(db.String(20), nullable=True)
    keywords = db.Column(db.String(500), nullable=True)
    location = db.Column(db.String(100), nullable=True)
    published_at = db.Column(db.DateTime, nullable=True)
    collected_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def set_keywords(self, keywords_list):
        self.keywords = json.dumps(keywords_list)
    
    def get_keywords(self):
        return json.loads(self.keywords) if self.keywords else []
    
    def __repr__(self):
        return f"SocialMediaPost('{self.platform}', '{self.author}')"

class Keyword(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    word = db.Column(db.String(100), nullable=False, unique=True)
    frequency = db.Column(db.Integer, default=0)
    last_updated = db.Column(db.DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"Keyword('{self.word}', {self.frequency})"

class Alert(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    keyword = db.Column(db.String(100), nullable=False)
    threshold = db.Column(db.Integer, nullable=False)
    email_notification = db.Column(db.Boolean, default=False)
    email = db.Column(db.String(120), nullable=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    
    def __repr__(self):
        return f"Alert('{self.keyword}', {self.threshold})"

class SentimentHistory(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date, nullable=False)
    positive_count = db.Column(db.Integer, default=0)
    negative_count = db.Column(db.Integer, default=0)
    neutral_count = db.Column(db.Integer, default=0)
    
    def __repr__(self):
        return f"SentimentHistory('{self.date}', +{self.positive_count}, -{self.negative_count}, ={self.neutral_count})" 